home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / GameboyDev / GBDK / lib / free.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-29  |  1.0 KB  |  44 lines

  1. /*
  2.   free.c
  3.   
  4.   Implementation of free()
  5. */
  6. #include <sys/malloc.h>
  7.  
  8. /*
  9.   free
  10.  
  11.   Attempts to free the memory pointed to by 'ptr'
  12.   Different from the standard free:  returns -1 if already free, or -2 if not part of the malloc list
  13. */
  14. BYTE free( void *ptr)
  15. {
  16.     /* Do a relativly safe free by only freeing vaild used hunks */
  17.     pmmalloc_hunk thisHunk;
  18.     
  19.     thisHunk = malloc_first;
  20.     
  21.     /* Adjust the pointer to point to the start of the hunk header - makes the comparision easier */
  22.     ptr = (void *)((UBYTE *)ptr - sizeof(mmalloc_hunk));
  23.     
  24.     /* Walk the linked list */
  25.     while (thisHunk && (thisHunk->magic==MALLOC_MAGIC)) {
  26.     /* Is this the hunk? */
  27.     if (thisHunk == ptr) {
  28.         debug("free", "Found hunk");
  29.         /* Only free it if it's used */
  30.         if (thisHunk->status == MALLOC_USED) {
  31.         thisHunk->status = MALLOC_FREE;
  32.         return 0;
  33.         }
  34.         debug("free", "Attempt to free a free hunk");
  35.         return -1;
  36.     }
  37.     /* walking... */
  38.     thisHunk = thisHunk->next;
  39.     };
  40.     
  41.     debug("free", "No hunk found");
  42.     return -2;
  43. }
  44.